package linkwechat.secirityhandler;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import linkwechat.pojo.SysUser;
import linkwechat.sysservice.SysUserService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

// 用户配置类
@Component
public class UserConfig implements UserDetailsService {

    @Resource
    SysUserService userService;

    //  根据用户名获取正确的信息
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", username);
        List<SysUser> list = userService.list(queryWrapper);
        if (list.size() != 0) {
            SysUser user = list.get(0);
            System.out.println(user);
            return user;
        } else {
            throw new UsernameNotFoundException("没有此用户");
        }

    }



}















